package com.example.administrator.x2picturetransliteration.Util.Img;

import android.graphics.Bitmap;
import android.widget.ImageView;

import com.squareup.picasso.Transformation;

/** 等比例裁剪 */

public class PicassoEqualProportionTransformation implements Transformation {

    private ImageView view; //显示图片的view

    public PicassoEqualProportionTransformation(ImageView view){

        this.view = view;
    }


    @Override
    public Bitmap transform(Bitmap source) {

        int targetWidth = view.getWidth();
        if (source.getWidth() == 0) {
            return source;
        }
        //如果图片大小大于等于设置的宽度，则按照设置的宽度比例来缩放
        double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
        int targetHeight = (int) (targetWidth * aspectRatio);
        if (targetHeight != 0 && targetWidth != 0) {
            Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
            if (result != source) {
                // Same bitmap is returned if sizes are the same
                source.recycle();
            }
            return result;
        } else {
            return source;
        }

    }

    @Override
    public String key() {
        return "";
    }


}
